package com.leyou.item.service;

import com.gitee.yongzhuzl.commonutil.util.empty.EmptyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * description: 品牌业务
 * @author 朱正磊
 * @date 2021-03-28 10:55:00
 */
@Service
public class BrandService {

    @Resource
    private BrandMapper brandMapper;

    /**
     * description: 分页查询品牌
     * @author 朱正磊
     * @date 2021-03-28 11:35:55
     * @param page 当前页，如果不传默认第1页
     * @param rows 每页多少条数据，如果不传默认每页5条数据
     * @param sortBy 排序字段，可以不传
     * @param desc 是否降序，true表示降序，false表示升序，如果不传默认false即升序
     * @param key 搜索条件，可以不传
     * @return 返回品牌分页结果
     */
    public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        // 分页
        PageHelper.startPage(page, rows);

        // 过滤
        Example example = new Example(Brand.class);
        if (EmptyUtil.isNotEmpty(key)) {
            // 封装过滤条件
            example.createCriteria().orLike("name", "%" + key + "%").orEqualTo("letter", key.toUpperCase());
        }

        // 排序
        if (EmptyUtil.isNotEmpty(sortBy)) {
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }

        // 查询
        /// Page<Brand> brandPage = (Page<Brand>) brandMapper.selectByExample(example);
        List<Brand> brandList = brandMapper.selectByExample(example);
        if (EmptyUtil.isEmpty(brandList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        // 解析分页信息
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);

        // 返回分页结果
        return new PageResult<>(pageInfo.getTotal(), brandList);
    }

    /**
     * description: 新增品牌
     * @author 朱正磊
     * @date 2021-03-28 23:48:37
     * @param brand 品牌对象
     * @param cids 类别id集合
     */
    @Transactional(rollbackFor = Throwable.class)
    public void saveBrand(Brand brand, List<Long> cids) {
        // 将id设置为null再进行新增确保主键是自增长
        brand.setId(null);
        Integer count = brandMapper.insert(brand);
        if (count == 0) {
            throw new LyException(ExceptionEnum.BRAND_SAVE_ERROR);
        }
        // 新增分类品牌中间表
        for (Long cid : cids) {
            count = brandMapper.insertCategoryBrand(cid, brand.getId());
            if (count == 0) {
                throw new LyException(ExceptionEnum.BRAND_SAVE_ERROR);
            }
        }
    }

    /**
     * description: 根据主键查询唯一品牌
     * @author 朱正磊
     * @date 2021-04-01 23:44:04
     * @param brandId 品牌id
     * @return 返回唯一品牌
     */
    public Brand queryById(Long brandId) {
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if (EmptyUtil.isEmpty(brand)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brand;
    }

    /**
     * description: 根据cid查询品牌
     * @author 朱正磊
     * @date 2021-04-02 13:47:48
     * @param cid 商品分类id
     * @return 返回品牌集合
     */
    public List<Brand> queryBrandByCid(Long cid) {
        List<Brand> brandList = brandMapper.queryByCategoryId(cid);
        if (EmptyUtil.isEmpty(brandList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brandList;
    }

    /**
     * description: 根据品牌id集合批量查询品牌集合
     * @author 朱正磊
     * @date 2021-04-09 17:06:02
     * @param ids 品牌id集合
     * @return 返回品牌集合
     */
    public List<Brand> queryByIds(List<Long> ids) {
        List<Brand> brandList = brandMapper.selectByIdList(ids);
        if (EmptyUtil.isEmpty(brandList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brandList;
    }

}
